package com.xinongbest.mall.service.impl;

import com.xinongbest.mall.common.Constant;
import com.xinongbest.mall.exception.ImoocMallException;
import com.xinongbest.mall.exception.ImoocMallExceptionEnum;
import com.xinongbest.mall.model.dao.CartMapper;
import com.xinongbest.mall.model.dao.ProductMapper;
import com.xinongbest.mall.model.dto.CartDto;
import com.xinongbest.mall.model.pojo.Cart;
import com.xinongbest.mall.model.pojo.Product;
import com.xinongbest.mall.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    ProductMapper productMapper;

    @Autowired
    CartMapper cartMapper;

    @Override
    public List<CartDto> list(Integer userId) {
        List<CartDto> cartDtoList = cartMapper.selectList(userId);
        if (!CollectionUtils.isEmpty(cartDtoList)) {
            cartDtoList.forEach(cartDto -> {
                cartDto.setTotalPrice(cartDto.getQuantity() * cartDto.getPrice());
            });
        }
        return cartDtoList;
    }

    @Override
    public List<CartDto> addCart(Integer userId, Integer productId, Integer number) {
        validationProduct(productId, number);
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if (cart != null) {
            number = cart.getQuantity() + number;
            Cart newCart = new Cart();
            newCart.setProductId(productId);
            newCart.setQuantity(number);
            newCart.setUserId(userId);
            newCart.setId(cart.getId());
            newCart.setSelected(Constant.CartProductStatus.CHECKED);
            cartMapper.updateByPrimaryKeySelective(newCart);
        } else {
            cart = new Cart();
            cart.setUserId(userId);
            cart.setProductId(productId);
            cart.setQuantity(number);
            cart.setSelected(Constant.CartProductStatus.CHECKED);
            cartMapper.insertSelective(cart);
        }
        return cartMapper.selectList(userId);
    }

    /**
     * 校验添加购物车商品：是否可售、是否存在、是否有库存
     *
     * @param productId
     */
    private void validationProduct(Integer productId, Integer number) {
        Product product = productMapper.selectByPrimaryKey(productId);
        if (product == null || product.getStatus().equals(Constant.SaleStatus.NOT_SALE)) {
            throw new ImoocMallException(ImoocMallExceptionEnum.NOT_SALE);
        }
        if (number > product.getStock()) {
            throw new ImoocMallException(ImoocMallExceptionEnum.NOT_ENOUGH);
        }
    }

    @Override
    public List<CartDto> update(Integer userId, Integer productId, Integer number) {
        validationProduct(productId, number);
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if (cart != null) {
            Cart newCart = new Cart();
            newCart.setProductId(productId);
            newCart.setQuantity(number);
            newCart.setId(cart.getId());
            newCart.setSelected(Constant.CartProductStatus.CHECKED);
            newCart.setUserId(userId);
            cartMapper.updateByPrimaryKeySelective(newCart);
        } else {
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }
        return cartMapper.selectList(userId);
    }

    @Override
    public List<CartDto> delete(Integer userId, Integer productId) {
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if (cart == null) {
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        } else {
            cartMapper.deleteByPrimaryKey(cart.getId());
        }
        return cartMapper.selectList(userId);
    }

    @Override
    public List<CartDto> selectOrNot(Integer userId, Integer productId, Integer selected) {
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if (cart == null) {
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        } else {
            cartMapper.selectOrNot(userId, productId, selected);
        }
        return cartMapper.selectList(userId);
    }

    @Override
    public List<CartDto> selectAllOrNot(Integer userId, Integer selected) {
        cartMapper.selectAllOrNot(userId, selected);
        return cartMapper.selectList(userId);
    }
}
